package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.BigProject;
import com.ruoyi.system.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Contract;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 新签合同Controller
 * 
 * @author xuliang
 * @date 2024-04-12
 */
@RestController
@RequestMapping("/system/contract")
public class ContractController extends BaseController
{
    @Autowired
    private IContractService contractService;
    @Autowired
    private IProjectInfoService projectInfoService;
    @Autowired
    private IProcurementDetailService procurementDetailService;
    @Autowired
    private ILaborService laborService;
    @Autowired
    private IDirectService directService;
    @Autowired
    private IIndirectService indirectService;
    @Autowired
    private IOutputService outputService;
    @Autowired
    private BigProjectService bigProjectService;

    /**
     * 查询新签合同列表
     */
    @PreAuthorize("@ss.hasPermi('system:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(Contract contract)
    {
        startPage();
        List<Contract> list = contractService.selectContractList(contract);
        return getDataTable(list);
    }

    /**
     * 导出新签合同列表
     */
    @PreAuthorize("@ss.hasPermi('system:contract:export')")
    @Log(title = "十三局所有项目信息汇总表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        BigProject bigProject;
        List<String> projectCodes = projectInfoService.getProjectCodes(null);
        for(String projectCode : projectCodes){
            bigProject = new BigProject();
            bigProject.setProjectCodeProject(projectCode);
            bigProjectService.insertBigProject(bigProject);
        }
        bigProjectService.updateBigProjectBeforeGet();
        List<BigProject> list = bigProjectService.selectBigProjectList(new BigProject());
        ExcelUtil<BigProject> util = new ExcelUtil<>(BigProject.class);
        util.exportExcel(response,list,"十三局所有项目信息汇总表");

    }

    /**
     * 获取新签合同详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(contractService.selectContractById(id));
    }

    /**
     * 新增新签合同
     */
    @PreAuthorize("@ss.hasPermi('system:contract:add')")
    @Log(title = "新签合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Contract contract)
    {

        if(contractService.checkContractBeforeAdd(contract.getProjectName(), contract.getProjectCode()) != 0){
            return error("项目名或合同编号不可重复");
        }

        return toAjax(contractService.insertContract(contract));
    }

    /**
     * 修改新签合同
     */
    @PreAuthorize("@ss.hasPermi('system:contract:edit')")
    @Log(title = "新签合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Contract contract)
    {
        return toAjax(contractService.updateContract(contract));
    }

    /**
     * 删除新签合同
     */
    @PreAuthorize("@ss.hasPermi('system:contract:remove')")
    @Log(title = "新签合同", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(contractService.deleteContractByIds(ids));
    }
}
